﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Message.cs" company="DarkBone">
//   DarkBone
// </copyright>
// <summary>
//   Defines the NetworkingMessage type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DarkBone.Engine.Networking
{
   using System;
   using System.Collections.Generic;
   using System.Net.Sockets;
   using System.Threading;

   /// <summary>
   /// Done event
   /// </summary>
   /// <param name="message">The message.</param>
   public delegate void Done(Message message);

   /// <summary>
   /// Netwokring message
   /// </summary>
   public class Message
   {
      /// <summary>
      /// Field containg message buffer lenght
      /// </summary>
      private List<byte> lengthHeader = new List<byte>();

      /// <summary>
      /// Socket locker
      /// </summary>
      private ManualResetEvent locker = new ManualResetEvent(false);

      /// <summary>
      /// message buffer itself
      /// </summary>
      private List<byte> transmissionBuffer = new List<byte>();

      /// <summary>
      /// read buffer
      /// </summary>
      private byte[] buffer = new byte[1024];

      /// <summary>
      /// Gets or sets the length.
      /// </summary>
      public int Length
      {
         get
         {
            return BitConverter.ToInt32(this.lengthHeader.ToArray(), 0);
         }

         set
         {
            this.lengthHeader.AddRange(BitConverter.GetBytes(value));
         }
      }

      /// <summary>
      /// Gets or sets the sent.
      /// </summary>
      /// <value>
      /// The sent.
      /// </value>
      public int Sent { get; set; }

      /// <summary>
      /// Gets or sets the buffer.
      /// </summary>
      /// <value>
      /// The buffer.
      /// </value>
      public byte[] Buffer
      {
         get
         {
            return this.buffer;
         }

         set
         {
            this.buffer = value;
         }
      }

      /// <summary>
      /// Gets or sets the transmission buffer.
      /// </summary>
      /// <value>
      /// The transmission buffer.
      /// </value>
      public List<byte> TransmissionBuffer
      {
         get
         {
            return this.transmissionBuffer;
         }

         set
         {
            this.transmissionBuffer = value;
         }
      }

      /// <summary>
      /// Gets or sets the locker.
      /// </summary>
      /// <value>
      /// The locker.
      /// </value>
      public ManualResetEvent Locker
      {
         get
         {
            return this.locker;
         }

         set
         {
            this.locker = value;
         }
      }

      /// <summary>
      /// Gets or sets the done delegate.
      /// </summary>
      /// <value>
      /// The done.
      /// </value>
      public Done Done { get; set; }

      /// <summary>
      /// Gets or sets the socket.
      /// </summary>
      /// <value>
      /// The socket.
      /// </value>
      public Socket Socket { get; set; }

      /// <summary>
      /// Gets or sets the length buffer.
      /// </summary>
      /// <value>
      /// The length buffer.
      /// </value>
      public List<byte> LengthHeader
      {
         get
         {
            return this.lengthHeader;
         }

         set
         {
            this.lengthHeader = value;
         }
      }

      /// <summary>
      /// Cleans this instance.
      /// </summary>
      public void Clean()
      {
         this.lengthHeader = new List<byte>();
         this.transmissionBuffer = new List<byte>();
         this.buffer = new byte[1024];
      }
   }
}
